package cn.tedu.jws.admin.service.impl;

;
import cn.tedu.jws.admin.ex.ServiceException;
import cn.tedu.jws.admin.mapper.BuldingsMapper;
import cn.tedu.jws.admin.pojo.dto.BuildingAddDto;
import cn.tedu.jws.admin.pojo.dto.BuildingUpdateDTO;
import cn.tedu.jws.admin.pojo.entity.Building;
import cn.tedu.jws.admin.pojo.vo.BuildingListVO;
import cn.tedu.jws.admin.pojo.vo.BuildingStandardVO;
import cn.tedu.jws.admin.service.BuildingService;
import cn.tedu.jws.admin.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.NumberFormat;
import java.util.List;

/**
 * @version 0.0.1
 * @Author cn.tedu
 * @Date 2023/03/25/15:55
 */
@Slf4j
@Service
public class BuildingServiceImpl implements BuildingService {
    @Autowired
    private BuldingsMapper buldingsMapper;

    @Override
    public void addNew(BuildingAddDto buildingAddDto) {
        log.debug("开始处理【添加楼栋】的业务，参数：{}", buildingAddDto);
        Long householder = buildingAddDto.getHouseholderId();
        Integer dong = buildingAddDto.getDong();
        Integer element = buildingAddDto.getElement();
        Integer roomNumber = buildingAddDto.getRoomNumber();

        //依据楼栋单元信息补位生成房间客户感知编号
        NumberFormat formatter = NumberFormat.getNumberInstance();
        formatter.setMinimumIntegerDigits(4);
        formatter.setGroupingUsed(false);
        String d = formatter.format(dong);
        String e = formatter.format(element);
        String r = formatter.format(roomNumber);
        StringBuilder num = new StringBuilder(6 + d + e + r);
        buildingAddDto.setNum(num.toString());

        //默认state=1正常状态
        buildingAddDto.setState(1);
        //检测输入是否合法
        if (dong < 0 || element < 0|| householder<0) {
            throw new ServiceException(ServiceCode.ERROR_BAD_REQUEST, "输入不合法");
        }
        // 调用Mapper对象的countByName()执行统计查询是否存在同一房间已登记
        int count = buldingsMapper.countByDongElementRoomNumber(dong, element, roomNumber);
        // 判断统计结果是否大于0,
        if (count > 0) {
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, "添加失败已存在该房屋信息!");
        }
        Building building = new Building();
        BeanUtils.copyProperties(buildingAddDto, building);
        log.debug("即将执行插入数据，参数：{}", building);
        buldingsMapper.insert(building);
    }

    @Override
    public void removeById(Long id) {
        log.debug("开始处理【删除楼栋】的业务，参数：{}", id);
        BuildingStandardVO building = buldingsMapper.getStandardById(id);
        if (building == null) {
            String message = "尝试删除的房间数据不存在，即将抛出异常";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        log.debug("即将删除{}房间", id);
        buldingsMapper.deleteById(id);
    }


    @Override
    public void updateById(Long id, BuildingUpdateDTO buildingUpdateDTO) {
        log.debug("开始处理【修改信息】的业务，ID：{}，新数据：{}", id, buildingUpdateDTO);
        BuildingStandardVO standardById = buldingsMapper.getStandardById(id);
        if (standardById == null) {
            String message = "尝试修改的属性数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        Integer dong = buildingUpdateDTO.getDong();
        Integer element = buildingUpdateDTO.getElement();
        Integer roomNumber = buildingUpdateDTO.getRoomNumber();
        int count = buldingsMapper.countByDongElementRoomNumberAndNotId(id,dong,element,roomNumber);
        if (count > 0) {
            String message = "修改失败,已存在该信息!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
        Building building = new Building();
        BeanUtils.copyProperties(buildingUpdateDTO, building);
        building.setId(id);
        buldingsMapper.update(building);
    }

    @Override
    public List<BuildingListVO> list() {
        log.debug("开始处理[查询楼栋列表]的业务,无参数");
        List<BuildingListVO> list = buldingsMapper.list();
        return list;
    }

    @Override
    public List<BuildingListVO> listByNum(String num) {
        log.debug("开始处理[根据编号查询楼栋列表]的业务,楼栋编号:{}",num);
        List<BuildingListVO> list = buldingsMapper.listByNum(num);
        return list;
    }

    @Override
    public List<BuildingListVO> listByDong(Integer dong) {
        log.debug("开始处理[根据楼栋查询楼栋列表]的业务,楼栋编号:{}", dong);
        List<BuildingListVO> list = buldingsMapper.listByDongList(dong);
        return list;
    }
}
